const Router = require('koa-router');
const fs = require('fs');

const router = new Router({
	// prefix: '/api'
});
let upload = async(ctx) => {
    let file = ctx.request.file; // 获取上传文件
    console.log('file', JSON.stringify(ctx.request.files))
    // 创建可读流
    const reader = fs.createReadStream(ctx.request.files.fwimage.path);
    let filePath = `E:/demos/koatest/test` + `/${ctx.request.files.fwimage.name} + ${Math.random()}`;
    // let remotefilePath = `http://www.xxxx.com:8887/img/my_blog_img` + `/${ctx.request.files['image']['name']}`;
    // // 创建可写流
    const upStream = fs.createWriteStream(filePath);
    // // 可读流通过管道写入可写流
    reader.pipe(upStream);
    return ctx.body = {
        // url: remotefilePath,
        url: filePath,
        message: "文件上传成功",
        cc: 0
    }
    // await new Promise((resolve, reject) => {
    //     setTimeout(() => {
    //         resolve();
    //         return ctx.body = '';
    //     }, 1000)
    // })
}
router.post('/upload', upload)

module.exports = router